/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pedirCita.java
 *
 * Created on 25-abr-2011, 22:43:43
 */

package interfaces;

import GestorUsuarios.IDException;
import GestorUsuarios.Medico;
import GestorUsuarios.UsuarioDAO;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import org.jdesktop.application.Action;
import Controlador.*;
import GestorUsuarios.Usuario;
import paciente.Interfaces;
import paciente.Interfaz;
import paciente.PacienteView;

/**
 *
 * @author Victor
 */
public class pedirCita extends javax.swing.JPanel {

    private PacienteView app;
    private JRadioButton [] lista_radio;
    private PropertyChangeListener eventos;
    private Usuario usuario;
    private Interfaces anterior;
    private UsuariosDisponibles [] disp;
    
    /** Creates new form pedirCita */
    public pedirCita(PacienteView a) {
        app = a;
        initComponents();

        fecha.setDate(Calendar.getInstance().getTime());
        lista_radio = new JRadioButton[6];

        

        lista_radio[0] = jRadioButton1;
        lista_radio[1] = jRadioButton2;
        lista_radio[2] = jRadioButton3;
        lista_radio[3] = jRadioButton4;
        lista_radio[4] = jRadioButton5;
        lista_radio[5] = jRadioButton6;

        eventos = null;
    }

    public void rellenaDatos(Interfaces interfaz, Usuario usu) {
        anterior = interfaz;
        usuario = usu;

        if(eventos==null)
            eventos = new PropertyChangeListener() {
		@Override
		public void propertyChange(PropertyChangeEvent e) {
                    try {
                        cargaDisponibles();
                    } catch (SQLException ex) {
                        Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NoSuchAlgorithmException ex) {
                        Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
                    }
		}
            };

        fecha.addPropertyChangeListener(eventos);
    }

    public void cargaDisponibles() throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(fecha.getDate());
        cal.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex());
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date ini = cal.getTime();
        cal.add(Calendar.HOUR_OF_DAY, 1);
        Date fin = cal.getTime();

        disp = UsuarioDAO.getMedicosDisponiblesOptimizado(ini, fin);
/*
        if(disp!=null) {
            for(int i=0;i<disp.length;i++) {
                System.out.println("Desde "+disp[i].getFechaInicio()+" a "+
                       disp[i].getFechaFin()+ " son:" );
                for(int j=0;j<disp[i].getArrayId().size();j++)
                    System.out.println(disp[i].getArrayId().get(j));
            }
        }*/


        cal.setTime(fecha.getDate());
        cal.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex());
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        ini = cal.getTime();
        cal.add(Calendar.MINUTE, 10);
        fin = cal.getTime();

        boolean primero_seleccionado = false;
        for(int i=0;i<6;i++) {
            UsuariosDisponibles d = UsuariosDisponibles.buscarPorRangoTiempo(disp, ini, fin);
            if(d != null) {
                if(d.getArrayId().size() > 0) {
                    lista_radio[i].setEnabled(true);
                    if(primero_seleccionado == false) {
                        lista_radio[i].setSelected(true);
                        primero_seleccionado = true;
                    }
                } else lista_radio[i].setEnabled(false);
            } else
                lista_radio[i].setEnabled(false);
        }


    }

      

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        aceptar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        horas = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        fecha = new com.toedter.calendar.JCalendar();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(paciente.PacienteApp.class).getContext().getResourceMap(pedirCita.class);
        aceptar.setFont(resourceMap.getFont("aceptar.font")); // NOI18N
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setText(resourceMap.getString("aceptar.text")); // NOI18N
        aceptar.setName("aceptar"); // NOI18N
        aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        horas.setFont(Interfaz.setFontSize());
        horas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00" }));
        horas.setName("horas"); // NOI18N
        horas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                horasActionPerformed(evt);
            }
        });

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setFont(Interfaz.setFontSize());
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.setName("jRadioButton1"); // NOI18N

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setFont(Interfaz.setFontSize());
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setName("jRadioButton2"); // NOI18N

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setFont(Interfaz.setFontSize());
        jRadioButton3.setText(resourceMap.getString("jRadioButton3.text")); // NOI18N
        jRadioButton3.setName("jRadioButton3"); // NOI18N

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setFont(Interfaz.setFontSize());
        jRadioButton4.setText(resourceMap.getString("jRadioButton4.text")); // NOI18N
        jRadioButton4.setName("jRadioButton4"); // NOI18N

        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setFont(Interfaz.setFontSize());
        jRadioButton5.setText(resourceMap.getString("jRadioButton5.text")); // NOI18N
        jRadioButton5.setName("jRadioButton5"); // NOI18N

        buttonGroup1.add(jRadioButton6);
        jRadioButton6.setFont(Interfaz.setFontSize());
        jRadioButton6.setText(resourceMap.getString("jRadioButton6.text")); // NOI18N
        jRadioButton6.setName("jRadioButton6"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        fecha.setName("fecha"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 297, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(344, 344, 344))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(horas, 0, 406, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(horas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(paciente.PacienteApp.class).getContext().getActionMap(pedirCita.class, this);
        jButton1.setAction(actionMap.get("cancelar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aceptar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(aceptar)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed
        Calendar cal = Calendar.getInstance();
        cal.setTime(fecha.getDate());
        cal.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex());
        cal.set(Calendar.SECOND, 0);

        boolean realizar = true;

        if(jRadioButton1.isSelected())
            cal.set(Calendar.MINUTE, 0);
        else if(jRadioButton2.isSelected())
            cal.set(Calendar.MINUTE, 10);
        else if(jRadioButton3.isSelected())
            cal.set(Calendar.MINUTE, 20);
        else if(jRadioButton4.isSelected())
            cal.set(Calendar.MINUTE, 30);
        else if(jRadioButton5.isSelected())
            cal.set(Calendar.MINUTE, 40);
        else if(jRadioButton6.isSelected())
            cal.set(Calendar.MINUTE, 50);
        else {
            realizar = false;
            app.limpiaErrores();
            app.addError("Seleccione alguna franja.");
            app.muestraListaErrores();
        }
        if(realizar==true) {
            String res = null;
            try {
                res = Controlador.pedirCita(usuario.getIdUsuario(), cal.getTime());
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IDException ex) {
                Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
            }

            if(res==null) {
                    app.abrirMensajeExito("Cita pedida correctamente", anterior);
                } else {
                    app.abrirMensajeError(res, anterior);
                }
        }

}//GEN-LAST:event_aceptarActionPerformed

    private void horasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_horasActionPerformed
        try {
            cargaDisponibles();
        } catch (SQLException ex) {
            Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(pedirCita.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_horasActionPerformed

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        app.volver(anterior);
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.ButtonGroup buttonGroup1;
    private com.toedter.calendar.JCalendar fecha;
    private javax.swing.JComboBox horas;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    // End of variables declaration//GEN-END:variables

}
